<?php
class M_rab extends CI_Model {
	
	function getlist($filter,$start,$end){
		$sql = "SELECT anggaran_id,
				(SELECT count(*) FROM m_anggaran as count_parent where anggaran_parent_id = m_anggaran.anggaran_id and 
				anggaran_aktif = 'y'
				) as child,
				anggaran_sat1,anggaran_sat2,anggaran_sat3,anggaran_sat4,
				anggaran_vol1,anggaran_vol2,anggaran_vol3,anggaran_vol4,anggaran_kegiatan,
				CONCAT(anggaran_kd1,'.',anggaran_kd2, '.',anggaran_kd3,'.',anggaran_kd4,'.',anggaran_kd5,'.', anggaran_kd6)
				as kode,
			concat( repeat('--',anggaran_level-1),anggaran_kegiatan)as kegiatan
			,TRIM(
				CONCAT(
					IF(anggaran_vol1 IS NULL,'' ,anggaran_vol1),' ',anggaran_sat1,' ',
					IF(anggaran_vol2 IS NULL,'' ,anggaran_vol2),' ',anggaran_sat2,' ',
					IF(anggaran_vol3 IS NULL,'' ,anggaran_vol3),' ',anggaran_sat3,' ',
					IF(anggaran_vol4 IS NULL,'' ,anggaran_vol4),' ',anggaran_sat4)
			) as satuan,
			anggaran_value,
			anggaran_value * (
				IF(anggaran_vol1 IS NULL, 1,anggaran_vol1) *
				IF(anggaran_vol2 IS NULL, 1,anggaran_vol2) *
				IF(anggaran_vol3 IS NULL, 1,anggaran_vol3) *
				IF(anggaran_vol4 IS NULL, 1,anggaran_vol4) 
			) as anggaran_total
			FROM m_anggaran
			WHERE anggaran_kd1 is not null AND anggaran_aktif = 'y'";
		$query = $this->db->query($sql);
		$total = $query->num_rows();
		
		if ($filter<>""){
				$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
				$sql .= " (lower(anggaran_kegiatan) LIKE '%".addslashes(strtolower($filter))."%' )";
		}
		$sql .= " ORDER BY anggaran_kd1, anggaran_kd2, anggaran_kd3, anggaran_kd4, anggaran_kd5, anggaran_kd6";
		$limit = $sql." LIMIT ".$start.", ".$end;
		$query = $this->db->query($limit);
		$arr = array();
		foreach($query->result() as $q){
			$arr[] = $q; 
		}
	
		$final = array(
			'total'   => $total,
			'results' => $arr
		);
		return json_encode($final);
	}
	
	function save($data){
		extract($data);
		$rab_kode_arr = explode('.',$rab_kode);
		$rab_level = count($rab_kode_arr);
		
		// set kode //
		$rab_kode1 = (@$rab_kode_arr[0] == null)? 0:$rab_kode_arr[0];
		$rab_kode2 = (@$rab_kode_arr[1] == null)? 0:$rab_kode_arr[1];
		$rab_kode3 = (@$rab_kode_arr[2] == null)? 0:$rab_kode_arr[2];
		$rab_kode4 = (@$rab_kode_arr[3] == null)? 0:$rab_kode_arr[3];
		$rab_kode5 = (@$rab_kode_arr[4] == null)? 0:$rab_kode_arr[4];
		$rab_kode6 = (@$rab_kode_arr[5] == null)? 0:$rab_kode_arr[5];
		
		$rab_vol1 = ($rab_satuan[0]== "" ? null : $rab_satuan[0]);
		$rab_sat1 = $rab_satuan[1];
		$rab_vol2 = ($rab_satuan[2]== "" ? null : $rab_satuan[2]);
		$rab_sat2 = $rab_satuan[3];
		$rab_vol3 = ($rab_satuan[4]== "" ? null : $rab_satuan[4]);
		$rab_sat3 = $rab_satuan[5];
		$rab_vol4 = ($rab_satuan[6]== "" ? null : $rab_satuan[6]);
		$rab_sat4 = $rab_satuan[7];
		
		// check parent //
		$parent_id = -1;
		$up = 0;
		// while( $parent_id != 0 ){
		$sql = "SELECT anggaran_id FROM m_anggaran ";
			
		for($i = $rab_level -1 ; $i > 0 ; $i--){
			$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
			$sql .= " anggaran_kd".$i." = '".$rab_kode_arr[$i - 1]."'";
		}  
		
		$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
		$sql .= " anggaran_level = '".($rab_level - 1)."'";
		
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0)
			$parent_id = $query->row('anggaran_id');
		// }
		// siapkan data submit //
		$data_submit = array(
			'anggaran_parent_id' => $parent_id,
			'anggaran_level' => $rab_level,
			'anggaran_kegiatan' =>  $rab_kegiatan,
			'anggaran_value' =>  $rab_harga,
			'anggaran_kd1' => $rab_kode1,
			'anggaran_kd2' => $rab_kode2,
			'anggaran_kd3' => $rab_kode3,
			'anggaran_kd4' => $rab_kode4,
			'anggaran_kd5' => $rab_kode5,
			'anggaran_kd6' => $rab_kode6,
			'anggaran_vol1' => $rab_vol1,
			'anggaran_vol2' => $rab_vol2,
			'anggaran_vol3' => $rab_vol3,
			'anggaran_vol4' => $rab_vol4,
			'anggaran_sat1' => $rab_sat1,
			'anggaran_sat2' => $rab_sat2,
			'anggaran_sat3' => $rab_sat3,
			'anggaran_sat4' => $rab_sat4,
			'anggaran_tahun' => date('Y')
		);
		$this->db->insert('m_anggaran',$data_submit);
		echo 1;
	}
	
	function update($data){
		extract($data);
		$rab_kode_arr = explode('.',$rab_kode);
		$rab_level = count($rab_kode_arr);
		
		// set kode //
		$rab_kode1 = (@$rab_kode_arr[0] == null)? 0:$rab_kode_arr[0];
		$rab_kode2 = (@$rab_kode_arr[1] == null)? 0:$rab_kode_arr[1];
		$rab_kode3 = (@$rab_kode_arr[2] == null)? 0:$rab_kode_arr[2];
		$rab_kode4 = (@$rab_kode_arr[3] == null)? 0:$rab_kode_arr[3];
		$rab_kode5 = (@$rab_kode_arr[4] == null)? 0:$rab_kode_arr[4];
		$rab_kode6 = (@$rab_kode_arr[5] == null)? 0:$rab_kode_arr[5];
		
		$rab_vol1 = ($rab_satuan[0]== "" ? null : $rab_satuan[0]);
		$rab_sat1 = $rab_satuan[1];
		$rab_vol2 = ($rab_satuan[2]== "" ? null : $rab_satuan[2]);
		$rab_sat2 = $rab_satuan[3];
		$rab_vol3 = ($rab_satuan[4]== "" ? null : $rab_satuan[4]);
		$rab_sat3 = $rab_satuan[5];
		$rab_vol4 = ($rab_satuan[6]== "" ? null : $rab_satuan[6]);
		$rab_sat4 = $rab_satuan[7];
		
		// check parent //
		$parent_id = -1;
		$up = 0;
		// while( $parent_id != 0 ){
		$sql = "SELECT anggaran_id FROM m_anggaran ";
			
		for($i = $rab_level -1 ; $i > 0 ; $i--){
			$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
			$sql .= " anggaran_kd".$i." = '".$rab_kode_arr[$i - 1]."'";
		}  
		
		$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
		$sql .= " anggaran_level = '".($rab_level - 1)."'";
		
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0)
			$parent_id = $query->row('anggaran_id');
		// }
		// siapkan data submit //
		$data_submit = array(
			'anggaran_parent_id' => $parent_id,
			'anggaran_level' => $rab_level,
			'anggaran_kegiatan' =>  $rab_kegiatan,
			'anggaran_value' =>  $rab_harga,
			'anggaran_kd1' => $rab_kode1,
			'anggaran_kd2' => $rab_kode2,
			'anggaran_kd3' => $rab_kode3,
			'anggaran_kd4' => $rab_kode4,
			'anggaran_kd5' => $rab_kode5,
			'anggaran_kd6' => $rab_kode6,
			'anggaran_vol1' => $rab_vol1,
			'anggaran_vol2' => $rab_vol2,
			'anggaran_vol3' => $rab_vol3,
			'anggaran_vol4' => $rab_vol4,
			'anggaran_sat1' => $rab_sat1,
			'anggaran_sat2' => $rab_sat2,
			'anggaran_sat3' => $rab_sat3,
			'anggaran_sat4' => $rab_sat4
		);
		$this->db->where('anggaran_id = "'.$rab_id.'"');
		$this->db->update('m_anggaran',$data_submit);
		echo 1;
	}
	
	function delete($data){
		$data_update = array(
			'anggaran_aktif' => 't'
		);
		$this->db->where($data);
		$this->db->update('m_anggaran',$data_update);
		echo 1;
	}
}